package fishBids.group3.behaviour.bidder;

import jade.core.behaviours.OneShotBehaviour;
import jade.core.behaviours.SimpleBehaviour;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import fishBids.group3.Bidder3;
import fishBids.group3.common.Constants;

public class AuctionOverBidderBehaviour extends SimpleBehaviour {

	/**
	 * 
	 */
	private static final long serialVersionUID = -284773764555908942L;
	
	public AuctionOverBidderBehaviour(Bidder3 agBidder){
		this.agBidder = agBidder;
	}

	@Override
	public void action() {
		//System.out.println("Bidder>>> waiting for AuctionOver");
		MessageTemplate mt = MessageTemplate.and( 
			MessageTemplate.MatchLanguage(this.agBidder.getCodec().getName()), 
			MessageTemplate.and(MessageTemplate.MatchOntology(this.agBidder.getOntology().getName()),
			MessageTemplate.and(MessageTemplate.MatchProtocol(Constants.AUCTION_OVER),
			MessageTemplate.MatchPerformative(ACLMessage.INFORM)))); 
					
		ACLMessage msg = this.agBidder.receive(mt); 
						
		if (msg != null) {
//			System.out.println("Bidder>>> receive AuctionOver");	
			this.agBidder.doDelete();
			System.out.println("bidder3: leaves the system");
			end = true;
		}
		block();
		
	}
	public boolean done()
	{
		return end;
	}
	
	Bidder3 agBidder;
	boolean end;
}
